const app = getApp();
Page({
  data: {

  },
  onLoad: function (options) {
    let that = this;
    that.setData({
      options: options,
    })
    that.getInfo(options.id, options.num);
  },
  getInfo(goods_id, goods_num){
    let that = this;
    wx.showLoading({
      title: '加载中...',
    })
    app.request('score.order/placeOrder', { goods_id, goods_num }, 'GET', false, function (res) {
      that.setData({
        pageShow: true,
        pay_price: res.data.data.pay_price,
        pay_score: res.data.data.pay_score,
        address: res.data.data.address,
        goods: res.data.data.goods,
      })
    })
  },
  onShow(){
    let that = this;
    if (wx.getStorageSync('addressChoose')) {
      that.setData({
        address: wx.getStorageSync('addressChoose'),
      }, () => {
        wx.removeStorageSync('addressChoose');
      })
    }
  },
  getFormid:function(e){
    app.getFormid(e);
  },
  skipPage:function(e){
    let that = this;
    let url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },
  bzInput:function(e){
    let that = this;
    let bz = e.detail.value;
    that.setData({
      bz: bz
    })
  },
  sure:function(e){
    let that = this;
    let goods_id = that.data.options.id;
    let address_id = that.data.address.id;
    let total_num = that.data.options.num || 1;
    wx.showModal({
      title: '确认兑换',
      content: '确认兑换改商品？',
      success:(res)=>{
        if(res.confirm){
          app.request('score.order/saveOrder',{
            goods_id, address_id, total_num
          },'GET',false,function(res){
            if (res.data.data.order_id){
              let order_id = res.data.data.order_id;
              app.request('pay/payScoreOrder', { order_id }, 'GET', false, function (res) {
                wx.requestPayment({
                  'timeStamp': res.data.data.timeStamp,
                  'nonceStr': res.data.data.nonceStr,
                  'package': res.data.data.package,
                  'signType': res.data.data.signType,
                  'paySign': res.data.data.paySign,
                  success: function (res) {
                    wx.showToast({
                      title: '兑换成功',
                      mask: true,
                    });
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 2,
                      })
                    }, 2000)
                  },
                });
              })
            }else{
              wx.showToast({
                title: '兑换成功',
                mask: true,
              });
              setTimeout(function () {
                wx.navigateBack({
                  delta: 2,
                })
              }, 2000)
            }
          })
        }
      }
    })
  }
})